﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using Semantics;
using Semantics.Base;

namespace Semantics.Example.ParserExample
{
    public partial class ShowInterfaceForm : Form
    {
        public class ListItem
        {
            private string id = string.Empty;
            private string name = string.Empty;
            public ListItem(string sid, string sname)
            {
                id = sid;
                name = sname;
            }
            public override string ToString()
            {
                return this.name;
            }
            public string ID
            {
                get
                {
                    return this.id;
                }
                set
                {
                    this.id = value;
                }
            }
            public string Name
            {
                get
                {
                    return this.name;
                }
                set
                {
                    this.name = value;
                }
            }
        }
        private SortedList<int, string> leafNodelist = new SortedList<int, string>();
        private SortedList<int, Semantics.Parse.AmbiguityResolver.LangNodeAmbiguities> ambiguitieslist;
        private int key;
        private string inputText;
        private ParserForm parentForm;
        private Semantics.Parse.AmbiguityResolver.resultOfCount interactiveBranch;

        public ShowInterfaceForm(string inputText, int key, SortedList<int, string> leafNodelist, SortedList<int, Semantics.Parse.AmbiguityResolver.LangNodeAmbiguities> ambiguitieslist, Semantics.Parse.AmbiguityResolver.resultOfCount interactiveBranch, ParserForm parentForm)
        {
            this.inputText=inputText;
            this.leafNodelist = leafNodelist;
            this.ambiguitieslist = ambiguitieslist;
            this.interactiveBranch = interactiveBranch;
            this.parentForm = parentForm;          
            InitializeComponent();
            ShowInterface(key);        

        }
       
        void ShowInterface(int key)
        {
            this.key = key;
            string value = interactiveBranch.interactiveNode.LangNode.Value;
            this.langNodeControl1.Nodes.Clear();
            this.listBox1.Items.Clear();
            string question = "请选出'" + value + "'分析正确的一组:";
            this.question.Text = question;
            string selectValue;
            string selectKey;
            foreach (KeyValuePair<int, LangNode> item in interactiveBranch.interactiveNode.Parents)
            {
                selectValue =item.Value.SemanticNodes[0].SN+ ":"+item.Value.Value;
                selectKey = item.Value.Value;
                //selectList = item.Value.Value;
                
                ListItem newitem = new ListItem(selectKey, selectValue);
                this.listBox1.Items.Add(newitem);
                this.listBox1.DisplayMember = "Name";
                this.listBox1.ValueMember = "Id";
               
                //在中标记是那个选项
                //SemanticNode selectNode = new SemanticNode();
                //this.langNodeControl1.Nodes.Add(selectNode);

                //selectNode.ShowCheckBox = true;
                //ShowLangNode(dictionary[key].Values[j], selectNode, 1);
                this.langNodeControl1.ShowSemanticNode(item.Value.SemanticNodes[0], this.langNodeControl1.Nodes, 0);

            }

                this.ButtonSubmit.Visible = true;
        }
        int findIndexInAmbiguitieslist(string key)
        {
            foreach (KeyValuePair<int, Semantics.Parse.AmbiguityResolver.LangNodeAmbiguities> item in ambiguitieslist)
            {
                if (string.Compare(key, item.Value.LangNode.Value) == 0)
                    return item.Key;
            }
            return -1;
        }

        private void ButtonSubmit_Click(object sender, EventArgs e)
        {
            this.Submit();
        }

        void Submit()
        {
            int selectIndex = this.listBox1.SelectedIndex;
            if (selectIndex == -1)
            {
                MessageBox.Show(this, "没有选择有效地项!");
                return;
            }
            int i;
            try
            {
                //string selectValue = this.listBox1.SelectedItem.ToString();
                string selectValue = ((ListItem)this.listBox1.SelectedItem).ID;
                int parentKey = 0;
                int parentSN = 0;

                if (string.Compare(selectValue, this.inputText.Trim()) == 0)
                {
                    foreach (KeyValuePair<int, LangNode> item in ambiguitieslist[key].Parents)
                    {
                        if (string.Compare(item.Value.Value, selectValue) == 0)
                        {
                            parentKey = item.Key;
                            break;
                        }
                    }
                    parentForm.showResult(ambiguitieslist[key].Parents[parentKey].SemanticNodes[0]);
                }
                else
                {
                    parentKey = findIndexInAmbiguitieslist(selectValue);
                    //标记选出来的分支结点的子节点IsScanned
                    int temp = 0;
                    foreach (KeyValuePair<int, LangNode> item in interactiveBranch.interactiveNode.Parents)
                    {
                        if (temp < selectIndex)
                            temp++;
                        else
                        {
                            parentSN = item.Value.SN;
                            break;
                        }
                    }
                    //将选出来的分支结点加入CorrectNodeList
                    Semantics.Parse.AmbiguityResolver resolver = new Semantics.Parse.AmbiguityResolver();
                    parentForm.CorrectNodeList = resolver.AddCorrectNode(ambiguitieslist[parentKey], parentKey, parentForm.CorrectNodeList);
                    //parentForm.CorrectNodeList[key].IsScanned = true;
                    List<LangNode> tempLangNodeList = interactiveBranch.interactiveNode.Parents[parentSN].Children;
                    int tempKey;
                    for (i = 0; i < tempLangNodeList.Count; i++)
                    {
                        tempKey = findIndexInAmbiguitieslist(tempLangNodeList[i].Value);
                        parentForm.CorrectNodeList[tempKey].IsScanned = true;
                    }
                    parentForm.CreatCorrectTree(parentKey);
                }
                this.Close();
                //findParent(selectValue);            
            }
            catch (Exception exp)
            {
                MessageBox.Show(this, exp.ToString());
            }
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            this.Submit();
        }

        private void langNodeControl1_DoubleClick(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        
        //public void findParent(string key)
        //{
        //    int index=findIndexInAmbiguitieslist(key);
        //    int parentIndex;
        //    if (ambiguitieslist[index].Parents.Count == 1)
        //    {
        //        parentIndex = ambiguitieslist[index].Parents.Values[0].SN;
        //        //如果该结点的父亲结点是根节点
        //        if (string.Compare(ambiguitieslist[index].Parents[parentIndex].Value, this.inputText) == 0)
        //        {
        //            this.Close();
        //           // ParserForm parserform = (ParserForm)this.Owner;
        //            parentForm.showResult(ambiguitieslist[index].Parents[parentIndex].SemanticNodes[0]);
        //            parentForm.Show();
        //        }
        //        //若果只有一个父亲结点、则找该父亲结点的父亲结点
        //        else
        //        {
        //            findParent(ambiguitieslist[index].Parents[parentIndex].Value);
        //        }
        //    }
        //    else
        //        ShowInterface(key);
        //}
        
    }
    
}
